Support DoRA (use_dora=True) in the safetensors LoRA merge#828
Support DoRA (use_dora=True) in the safetensors LoRA merge#828danielhanchen wants to merge 1 commit into
Conversation
The dense merge ignored the DoRA magnitude vector: create_lora_statistics did not capture it, so the key-consistency check raised 'Extracted keys do not match', and the merge math applied only W0 + alpha*BA. Now capture lora_magnitude_vector, ignore it in the key check, and fold it in _merge_lora as (m / ||W0 + alpha*BA||_row) * (W0 + alpha*BA), matching PEFT's DoRA merge. MoE-expert DoRA is refused (fail loud) since the expert helpers do not apply the magnitude. Test compares the dense merge against PEFT merge_and_unload.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
There was a problem hiding this comment.
Code Review
This pull request adds support for merging DoRA (use_dora=True) adapters in the safetensors merge path by rescaling the merged weight direction to the learned magnitude vector. It also ensures that DoRA magnitude weights are correctly ignored during key-consistency checks and explicitly refuses DoRA merges on MoE expert layers to prevent silent failures. A comprehensive test suite is introduced to validate these changes against PEFT's own DoRA merge. No review comments were provided, so there is no additional feedback.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Summary
DoRA (
use_dora=True) adapters could not be merged through the safetensors path.create_lora_statisticsdid not capture thelora_magnitude_vector, so the key-consistency check raisedUnsloth: Extracted keys = {...lora_magnitude_vector.default.weight} do not match!, and even past that the merge math applied onlyW0 + alpha*BA, dropping the magnitude.This adds DoRA support for the dense merge path (attention / MLP / any dense linear, including the language and vision/audio towers of multimodal models):
LoraStatsgains amagnitudefield;create_lora_statisticscaptureslora_magnitude_vector.default.assert_same_keysignores the magnitude tensor (folded into the weight, omitted from the merged checkpoint), likelora_A/lora_B._merge_lorafolds it in as(m / ||W0 + alpha*BA||_row) * (W0 + alpha*BA), i.e. one L2 norm per output row over the input dim, matching PEFT's DoRA merge.MoE-expert DoRA is explicitly refused with a clear error (the per-expert / fused expert helpers fold only the LoRA delta, not the magnitude), so a DoRA MoE merge fails loud instead of silently dropping the magnitude.
Test
tests/test_dora_merge.py:merge_and_unload(atol 1e-4) and actually changes the weight;use_dora=False) is unchanged (magnitude is None);Full merge e2e suite (dense + MoE fused/per-expert + resized) stays green (65 passed, 6 skipped).